package com.ss.android.article.base.feature.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import com.bytedance.common.utility.Logger;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.feed.activity.ArticleRecentFragment;
import com.ss.android.article.base.feature.model.CategoryItem;
import com.ss.android.article.base.feature.subscribe.b.SubscribeManager;
import com.ss.android.article.browser.main.BaseHomePageFragment;
import com.ss.android.common.app.AbsPagerAdapter;
import com.ss.android.common.app.UIScreenContext;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class SubscriptionPagerAdapter extends AbsPagerAdapter implements com.ss.android.article.base.feature.category.activity.CategoryTabStrip.a {
    public WeakReference a;
    public List<CategoryItem> mCategoryItemList;
    public ViewPager mViewPager;
    private int g = -1;
    private Field h = null;
    private HashMap i = new HashMap();
    private int j = 1;
    private a$a k;
    private com.b.a.a.a l;

    public SubscriptionPagerAdapter(FragmentManager fragmentManager, List<CategoryItem> categoryItemList, ViewPager viewPager, a$a arg8) {
        super(fragmentManager);
        this.mCategoryItemList = categoryItemList;
        AppData.getInstance();
        this.mViewPager = viewPager;
        this.k = arg8;
        try {
            this.h = AbsPagerAdapter.class.getDeclaredField("mFragmentTransaction");
            this.h.setAccessible(true);
        } catch (Exception v0) {
            Logger.w("CateAdapter", "get mCurTransaction Field exception: " + v0);
        }
    }

    public final int getCount() {
        return this.mCategoryItemList.size();
    }

    public final int getItemPosition(Object arg6) {
        int v1;
        int v2 = -2;
        if ((arg6 instanceof IRecentFragment)) {
            String v3 = ((IRecentFragment) arg6).getCategory();
            if (!android.support.design.a.isTextEmpty(v3)) {
                Iterator v4 = this.mCategoryItemList.iterator();
                v1 = 0;
                while (true) {
                    if (!v4.hasNext()) {
                        return v2;
                    } else if (!v3.equals(((CategoryItem) v4.next()).category)) {
                        ++v1;
                        continue;
                    }
                    return v1;
                }
            } else {
                return v2;
            }
        } else {
            label_20:
            v1 = v2;
        }

        return v1;
    }

    public final CharSequence getPageTitle(int arg2) {
        String v0 = arg2 < 0 || arg2 >= this.mCategoryItemList.size() ? "" : this.mCategoryItemList.get(arg2).name;
        return ((CharSequence) v0);
    }

    public final void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
        if ((object instanceof Fragment)) {
            try {
                if (this.mFragmentTransaction == null) {
                    return;
                }

                this.mFragmentTransaction.remove(((Fragment) object));
            } catch (Exception v0) {
                Logger.w("CateAdapter", "destroyItem remove fragment exception: " + v0);
            }
        }
    }

    public final Fragment createFragment(int position) {
        Fragment v0_1;
        Object v0 = this.mCategoryItemList.get(position);
        Logger.v("CateAdapter", "getItem " + ((CategoryItem) v0).category);
        Bundle v2 = new Bundle();
        v2.putString("category", ((CategoryItem) v0).category);
        v2.putInt("category_article_type", ((CategoryItem) v0).type);
        v2.putString("category_id", ((CategoryItem) v0).categoryId);
        try {
            v2.putLong("concern_id", Long.valueOf(((CategoryItem) v0).concernId).longValue());
        } catch (NumberFormatException v1) {

        }

        ArticleRecentFragment v1_1 = null;
        if (((CategoryItem) v0).type == 4) {
            if ("question_and_answer".equals(((CategoryItem) v0).category)) {
                v2.putInt("wenda_refer_type", 0);
            }
            v0_1 = new ArticleRecentFragment();
            ((ArticleRecentFragment) v0_1).setBridgeApi(this.l);
        } else {
            if (((CategoryItem) v0).type == 3) {
                v0_1 = new ArticleRecentFragment();
                ((ArticleRecentFragment) v0_1).setBridgeApi(this.l);
            } else if (((CategoryItem) v0).type == 1) {
                v0_1 = new ArticleRecentFragment();
                ((ArticleRecentFragment) v0_1).setBridgeApi(this.l);
            } else if (((CategoryItem) v0).category.equals("subscription")) {
                v0_1 = new com.ss.android.article.base.feature.subscribe.activity.e();
            } else {
                v0_1 = v1_1;
            }
        }

        if (v0_1 != null) {
            v0_1.setArguments(v2);
        }

        return v0_1;
    }

    public final CategoryItem b(int arg2) {
        Object v0;
        if (arg2 < 0 || arg2 >= this.mCategoryItemList.size()) {
            v0 = null;
        } else {
            v0 = this.mCategoryItemList.get(arg2);
        }

        return ((CategoryItem) v0);
    }

    public final void setPrimaryItem(ViewGroup container, int position, Object object) {
        String v0_4;
        String v5_1;
        Object v0_2;
        Object v1 = null;
        int v7 = 3;
        int v3 = 0;
        int v4 = 2;
        if (this.g != position) {
            Logger.v("CateAdapter", "setPrimaryItem " + position);
        }

        this.g = position;
        if (object != null) {
            if (this.a != null) {
                v1 = this.a.get();
            }

            if (v1 != object && ((v1 instanceof IRecentFragment))) {
                ((IRecentFragment) v1).onUnsetAsPrimaryPage(v4);
            }

            if (v1 == object) {
            } else {
                if (this.k != null) {
                    a$a v1_1 = this.k;
                    if (v1_1.a.o != 1 && !v1_1.a.p) {
                        v1_1.a.o = v4;
                    }

                    if (position >= 0 && position <= v1_1.a.mCategoryItemList.size()) {
                        Fragment v0 = v1_1.a.mSubscriptionPagerAdapter.findFragment(position);
                        if ((v0 instanceof IRecentFragment)) {
                            BaseHomePageFragment v5 = v1_1.a;
                            boolean v0_1 = ((IRecentFragment) v0).supportRefreshButton() == 1 ? true : false;
                            v5.n = v0_1;
                        }

                        if (!v1_1.a.p) {
                            v0_2 = v1_1.a.mCategoryItemList.get(position);
                            if (((CategoryItem) v0_2).category.equals("__all__")) {
                                v5_1 = "new_tab";
                                if (v1_1.a.mAppData.getAbSettings().isAppLogOld()) {
                                    if (v1_1.a.t == v4) {
                                        v1_1.a.a(v5_1, "enter_click", position);
                                    } else if (v1_1.a.t != v7) {
                                        v1_1.a.a(v5_1, "enter_flip", position);
                                    }
                                }

                                if (!v1_1.a.mAppData.getAbSettings().isAppLogNew()) {
                                } else {
                                    BaseHomePageFragment.a("enter_category", v1_1.a.t, ((CategoryItem) v0_2));
                                }
                            } else {
                                if (((CategoryItem) v0_2).category.equals("subscription")) {
                                    v5_1 = "subscription";
                                    SubscribeManager v0_3 = SubscribeManager.getInstance();
                                    if (v1_1.a.t == v4) {
                                        v0_4 = v0_3.c.k ? "enter_click_tip" : "enter_click";
                                    } else if (v1_1.a.t == v7) {
                                        v0_4 = "enter_panel";
                                    } else if (v0_3.c.k) {
                                        v0_4 = "enter_flip_tip";
                                    } else {
                                        v0_4 = "enter_flip";
                                    }

                                    v1_1.a.a(v5_1, v0_4, position);
                                } else {
                                    v5_1 = "category";
                                    if (v1_1.a.mAppData.getAbSettings().isAppLogOld()) {
                                        if (v1_1.a.t == v4) {
                                            v1_1.a.a(v5_1, "enter_click_" + ((CategoryItem) v0_2).category, position);
                                        } else if (v1_1.a.t != v7) {
                                            v1_1.a.a(v5_1, "enter_flip_" + ((CategoryItem) v0_2).category, position);
                                        }
                                    }

                                    if (!v1_1.a.mAppData.getAbSettings().isAppLogNew()) {

                                    } else {
                                        BaseHomePageFragment.a("enter_category", v1_1.a.t, ((CategoryItem) v0_2));
                                    }
                                }
                            }
                        }
                        v1_1.a.mSubscriptionPagerAdapter.l = v1_1.a;
                        v1_1.a.t = 1;
                        v1_1.a.p = false;
                    } else {
                        v1_1.a.t = 0;
                    }
                }

                this.a = new WeakReference(object);
                if (object instanceof IRecentFragment) {
                    ((IRecentFragment) object).onSetAsPrimaryPage(v4);
                }
            }
        } else {
            this.a = ((WeakReference) v1);
        }
        v0_2 = object;
        if (v0_2 != this.mFragment && v0_2 != null && ((v0_2 instanceof UIScreenContext)) && this.k != null) {
            HashMap v1_2 = new HashMap();
            a$a v5_2 = this.k;
            if (v5_2.a.o == 1) {
                v5_2.a.o = 0;
                v3 = 1;
            } else if (v5_2.a.o == v4) {
                v5_2.a.o = 0;
                v3 = v4;
            }

            if (v3 == 1) {
                ((Map) v1_2).put("enter_type", "click");
            } else if (v3 == 0) {
                ((Map) v1_2).clear();
            } else {
                ((Map) v1_2).put("enter_type", "flip");
            }

            ((UIScreenContext) v0_2).setEnterContext(((Map) v1_2));
        }

        super.setPrimaryItem(container, position, object);
    }

    public final long c(int arg5) {
        int v0_2;
        long v0_1;
        if (arg5 < 0 || arg5 >= this.mCategoryItemList.size()) {
            v0_1 = ((long) arg5);
        } else {
            Object v0 = this.mCategoryItemList.get(arg5);
            if ("__all__".equals(((CategoryItem) v0).category)) {
                v0_1 = 0;
            } else {
                Object v1 = this.i.get(((CategoryItem) v0).category);
                if (v1 != null) {
                    v0_2 = ((Integer) v1).intValue();
                } else {
                    int v1_1 = this.j;
                    this.i.put(((CategoryItem) v0).category, Integer.valueOf(v1_1));
                    ++this.j;
                    v0_2 = v1_1;
                }

                v0_1 = ((long) v0_2);
            }
        }

        return v0_1;
    }

    public final IRecentFragment d() {
        Object v0;
        IRecentFragment v1 = null;
        if (this.a != null) {
            v0 = this.a.get();
            if (!(v0 instanceof IRecentFragment)) {
                v0 = null;
            }
        } else {
            v0 = v1;
        }
        return ((IRecentFragment) v0);
    }

    public final String d(int arg4) {
        String v0 = arg4 < 0 || arg4 >= this.mCategoryItemList.size() ? super.d(arg4) : "cate_" + this.mCategoryItemList.get(arg4).category;
        return v0;
    }

    public final Fragment findFragment(int arg3) {
        return this.mFragmentManager.findFragmentByTag(this.getTag(this.mViewPager.getId(), arg3));
    }

    public static final class a$a {
        private final BaseHomePageFragment a;

        public a$a(BaseHomePageFragment arg1) {
            this.a = arg1;
        }
    }
}

